using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    /// <summary> 
    ///Author: daiduong19051986@gmail.com
    /// <summary>
    public class TVC_MVPDETDataAccess : Connection
    {
        #region Local Variable
        private string _strSPInsertName = "dbo.[procTVC_MVPDETPRV_add]";
        private string _strSPUpdateName = "dbo.[procTVC_MVPDETPRV_update]";
        private string _strSPDeleteName = "dbo.[procTVC_MVPDETPRV_delete]";
        private string _strSPGetName = "dbo.[procTVC_MVPDETPRV_get]";
        private string _strSPGetAllName = "dbo.[procTVC_MVPDETPRV_getall]";
        private string _strSPGetPages = "dbo.[procTVC_MVPDETPRV_getpaged]";
        private string _strSPIsExist = "dbo.[procTVC_MVPDETPRV_isexist]";
        private string _strTableName = "[TVC_MVPDETPRV]";
        private string _strSPGetTransferOutName = "dbo.[procTVC_MVPDETPRV_gettransferout]";
        private string _strSPGetByCode = "dbo.procTVC_MVPDETPRV_getbyCode";
        #endregion Local Variable

        #region Method
        public TVC_MVPDETInfo Get(string db, Int32 AUTO_ID, ref string sErr)
        {
            string sql = @"SELECT        AUTO_ID, Code, FieldName, SourceName, START, LENGTH, OPERATOR, VALUEF, VALUET
            FROM            TVC_MVPDET" + db + @"
            WHERE        (AUTO_ID = @AUTO_ID)";
            TVC_MVPDETInfo objEntr = new TVC_MVPDETInfo();
            InitConnect();
            InitCommand(sql);
            AddParameter(TVC_MVPDETInfo.Field.AUTO_ID.ToString(), AUTO_ID);

            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally { closeConnection(); }

            if (list.Rows.Count > 0)
                objEntr = new TVC_MVPDETInfo(list.Rows[0]);
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            if (sErr != "") ErrorLog.SetLog(sErr);
            return objEntr;
        }



        public DataTable GetAll(string db, ref string sErr)
        {
            string sql = @"SELECT        AUTO_ID, Code, FieldName, SourceName, START, LENGTH, OPERATOR, VALUEF, VALUET
            FROM            TVC_MVPDET" + db;
            InitConnect();
            InitCommand(sql);
            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally { closeConnection(); }


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        /// <summary>
        /// Return 1: Table is exist Identity Field
        /// Return 0: Table is not exist Identity Field
        /// Return -1: Erro
        /// </summary>
        /// <param name="tableName"></param>
        public Int32 Add(string db, TVC_MVPDETInfo objEntr, ref string sErr)
        {
            string sql = @"INSERT INTO TVC_MVPDET" + db + @"
                         (AUTO_ID, Code, FieldName, SourceName, START, LENGTH, OPERATOR, VALUEF, VALUET)
            VALUES        (@AUTO_ID,@Code,@FieldName,@SourceName,@START,@LENGTH,@OPERATOR,@VALUEF,@VALUET)
            SELECT SCOPE_IDENTITY()";
            int ret = -1;
            openConnection();
            InitCommand(sql);
            AddParameter(TVC_MVPDETInfo.Field.AUTO_ID.ToString(), objEntr.AUTO_ID);
            AddParameter(TVC_MVPDETInfo.Field.Code.ToString(), objEntr.Code);
            AddParameter(TVC_MVPDETInfo.Field.FieldName.ToString(), objEntr.FieldName);
            AddParameter(TVC_MVPDETInfo.Field.SourceName.ToString(), objEntr.SourceName);
            AddParameter(TVC_MVPDETInfo.Field.START.ToString(), objEntr.START);
            AddParameter(TVC_MVPDETInfo.Field.LENGTH.ToString(), objEntr.LENGTH);
            AddParameter(TVC_MVPDETInfo.Field.OPERATOR.ToString(), objEntr.OPERATOR);
            AddParameter(TVC_MVPDETInfo.Field.VALUEF.ToString(), objEntr.VALUEF);
            AddParameter(TVC_MVPDETInfo.Field.VALUET.ToString(), objEntr.VALUET);

            try
            {
                //command.ExecuteNonQuery();
                object tmp = executeScalar();
                if (tmp != null && tmp != DBNull.Value)
                    ret = Convert.ToInt32(tmp);
                else
                    ret = 0;
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally { closeConnection(); }
            if (sErr != "") ErrorLog.SetLog(sErr);

            return ret;
        }

        public string Update(string db, TVC_MVPDETInfo objEntr)
        {
            string sql = @"UPDATE       TVC_MVPDET" + db + @"
            SET                Code = @Code, FieldName = @FieldName, SourceName = @SourceName, START = @START, LENGTH = @LENGTH, OPERATOR = @OPERATOR, VALUEF = @VALUEF, VALUET = @VALUET
            WHERE        (AUTO_ID = @AUTO_ID)";
            openConnection();
            InitCommand(sql);
            AddParameter(TVC_MVPDETInfo.Field.AUTO_ID.ToString(), objEntr.AUTO_ID);
            AddParameter(TVC_MVPDETInfo.Field.Code.ToString(), objEntr.Code);
            AddParameter(TVC_MVPDETInfo.Field.FieldName.ToString(), objEntr.FieldName);
            AddParameter(TVC_MVPDETInfo.Field.SourceName.ToString(), objEntr.SourceName);
            AddParameter(TVC_MVPDETInfo.Field.START.ToString(), objEntr.START);
            AddParameter(TVC_MVPDETInfo.Field.LENGTH.ToString(), objEntr.LENGTH);
            AddParameter(TVC_MVPDETInfo.Field.OPERATOR.ToString(), objEntr.OPERATOR);
            AddParameter(TVC_MVPDETInfo.Field.VALUEF.ToString(), objEntr.VALUEF);
            AddParameter(TVC_MVPDETInfo.Field.VALUET.ToString(), objEntr.VALUET);

            string sErr = "";
            try
            {
                executeNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally { closeConnection(); }
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }

        public string Delete(string db, Int32 AUTO_ID)
        {
            string sql = @"DELETE TVC_MVPDET" + db + @"
	        WHERE 	AUTO_ID = @AUTO_ID";
            openConnection();
            InitCommand(sql);
            AddParameter(TVC_MVPDETInfo.Field.AUTO_ID.ToString(), AUTO_ID);

            string sErr = "";
            try
            {
                executeNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally { closeConnection(); }
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }


        public Boolean IsExist(string db, Int32 AUTO_ID)
        {
            string sql = @"SELECT        TOP (1) AUTO_ID
            FROM            TVC_MVPDET" + db + @"
            WHERE        (AUTO_ID = @AUTO_ID)";
            InitConnect();
            InitCommand(sql);
            AddParameter(TVC_MVPDETInfo.Field.AUTO_ID.ToString(), AUTO_ID);

            string sErr = "";
            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally { closeConnection(); }
            if (sErr != "") ErrorLog.SetLog(sErr);
            if (list.Rows.Count == 1)
                return true;
            return false;
        }

        private string CreateWhereClause(TVC_MVPDETInfo obj)
        {
            String result = "";

            return result;
        }

        public DataTable Search(string columnName, string columnValue, string condition, string tableName, ref string sErr)
        {
            string query = "select * from " + _strTableName + " where " + columnName + " " + condition + " " + columnValue;
            DataTable list = new DataTable();
            openConnection();
            try
            {
                list = executeSelectQuery(query);
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally { closeConnection(); }
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            //    if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        public DataTable GetTransferOut(string dtb, object from, object to, ref string sErr)
        {
            InitConnect();
            InitSPCommand(_strSPGetTransferOutName);
            AddParameter("DB", dtb);
            AddParameter("FROM", from);
            AddParameter("TO", to);
            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally { closeConnection(); }


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        #endregion Method


        public DataTable Get(string db, string Code, ref string sErr)
        {
            string sql = @"SELECT        AUTO_ID, Code, FieldName, SourceName, START, LENGTH, OPERATOR, VALUEF, VALUET
            FROM            TVC_MVPDET" + db + @"
            WHERE        (Code = @Code)";
            TVC_MVPDETInfo objEntr = new TVC_MVPDETInfo();
            InitConnect();
            InitCommand(sql);
            AddParameter(TVC_MVPDETInfo.Field.Code.ToString(), Code);

            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally { closeConnection(); }


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }

        public void DeletebyCode(string db, string Code, ref string sErr)
        {
            string sql = @"DELETE TVC_MVPDET" + db + @"
	        WHERE 	@Code = Code";
            openConnection();
            InitCommand(sql);
            AddParameter(TVC_MVPDETInfo.Field.Code.ToString(), Code);
            try
            {
                executeNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally { closeConnection(); }
            if (sErr != "") ErrorLog.SetLog(sErr);

        }
    }
}
